import ajax from "@/utils/ajax";
import { debounce } from 'lodash'
import { useInjectFormContext } from '@/components/form/context'

function useRemoteOptions(props,options){
    const remoteOptionsDebounce = debounce(remoteOptions,300)
    let form = useInjectFormContext()
    function remoteOptions(val = '') {
        return new Promise((resolve,reject) => {
            if(props.remote){
                let params = props.remote
                params.data.search = val
                if(form){
                    params.data.data = form.submitData()
                }
                ajax(params).then(res => {
                    options.value = res.data
                    resolve(res.data)
                }).catch(()=>{
                    reject()
                })
            }else{
                resolve(options.value)
            }
        })
    }
    return {remoteOptionsDebounce,remoteOptions}
}
export default useRemoteOptions